const Koa = require('koa');
const fs  = require('fs');

const app = new Koa();

async function render(page){
    return new Promise((resolve, reject) => {
        let view_url = './view/' + page;
        fs.readFile(view_url, "binary", (err, data) => {
            if(err){
                reject(err);
            }else{
                resolve(data);
            }
        })
    })
}

async function route(url){
    let page = '404.html';
    switch(url){
        case '/':
            page = 'index.html';
            break;
        case '/index.html':
            page = 'index.html';
            break;
        case '/404':
            page = '404.html';
            break;
        default:
            break;
    }
    let html = await render(page);
    return html;
}




app.use(async (ctx) => {
    console.log(ctx.request.url);
    let html = await route(ctx.request.url);
    ctx.body = html;
})

app.listen(3000);
console.log('starting...');
